FROM node:10.12.0-alpine as web-builder
VOLUME /tmp
RUN mkdir -p /opt/
WORKDIR /opt/
COPY package.json .
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk add --update git
RUN git config --global url."https://".insteadOf git://
RUN npm config set sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
RUN npm config set registry http://registry.npm.taobao.org/
RUN npm install --unsafe-perm
COPY static ./static
COPY vue ./vue
COPY webpack.config.js .
RUN npm run build

FROM goreng/golang-cross-builder:v1.15.1 as go-builder
VOLUME /tmp
USER root
RUN mkdir -p /opt
COPY linuxx64_odbc_cli.tar.gz /opt/clidriver.tar.gz
WORKDIR /opt
RUN tar zxvf /opt/clidriver.tar.gz

# Allows you to add additional packages via build-arg
ARG ADDITIONAL_PACKAGE
ARG CGO_ENABLED=1
ARG GO111MODULE="on"
ARG GOPROXY="https://goproxy.cn,direct"
ARG GOFLAGS=""
ENV CGO_ENABLED=1
RUN export DB2HOME=/opt/clidriver
RUN export CGO_CFLAGS=-I/opt/clidriver/include
RUN export CGO_LDFLAGS=-L/opt/clidriver/lib

#RUN  sed -i 's#http://deb.debian.org#https://mirrors.163.com#g' /etc/apt/sources.list
RUN rm -f /etc/apt/sources.list

RUN echo "deb http://mirrors.aliyun.com/debian/ buster main non-free contrib" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.aliyun.com/debian-security buster/updates main" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.aliyun.com/debian-security buster/updates main" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib" >> /etc/apt/sources.list
RUN echo "deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib" >> /etc/apt/sources.list

RUN  apt-get clean



RUN apt-get update
RUN apt-get install -y libxml2-dev




RUN mkdir -p /go/src
COPY go.mod /go/src/
WORKDIR /go/src
RUN  CGO_ENABLED=${CGO_ENABLED} GOOS=linux go mod download

COPY main.go /go/src/
COPY db2 /go/src/db2
COPY es /go/src/es
COPY job /go/src/job
COPY model /go/src/model
COPY routers /go/src/routers
COPY service /go/src/service
COPY controllers /go/src/controllers

RUN CGO_ENABLED=${CGO_ENABLED} GOARCH=amd64 GOOS=linux CGO_CFLAGS=-I/opt/clidriver/include CGO_LDFLAGS=-L/opt/clidriver/lib   go build --ldflags "-s -w" -a -installsuffix cgo -o main main.go

FROM ubuntu
LABEL MAINTAINER="awol2005ex@163.com"
USER root
RUN mkdir -p /opt
WORKDIR /opt
COPY linuxx64_odbc_cli.tar.gz /opt/clidriver.tar.gz

RUN tar zxvf /opt/clidriver.tar.gz
RUN export DB2HOME=/opt/clidriver
RUN export CGO_CFLAGS=-I/opt/clidriver/include
RUN export CGO_LDFLAGS=-L/opt/clidriver/lib
RUN rm -f /opt/clidriver.tar.gz
RUN mkdir -p /go/src
WORKDIR /go/src
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone



RUN  sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
RUN  apt-get clean



RUN  apt-get update
RUN  apt-get install -y libxml2-dev
COPY --from=go-builder /go/src/main ./
COPY --from=web-builder /opt/static ./static
RUN chmod +x main

EXPOSE 8080

ENTRYPOINT    LD_LIBRARY_PATH=/opt/clidriver/lib ./main